home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Produtividade / OpenOffice.org 2.0.1 / openofficeorg4.cab / test_mailbox.py < prev    next >
Text File  |  2005-11-19  |  3KB  |  105 lines

  1. import mailbox
  2. import os
  3. import time
  4. import unittest
  5. from test import test_support
  6.  
  7. # cleanup earlier tests
  8. try:
  9.     os.unlink(test_support.TESTFN)
  10. except os.error:
  11.     pass
  12.  
  13.  
  14. DUMMY_MESSAGE = """\
  15. From: some.body@dummy.domain
  16. To: me@my.domain
  17.  
  18. This is a dummy message.
  19. """
  20.  
  21.  
  22. class MaildirTestCase(unittest.TestCase):
  23.  
  24.     def setUp(self):
  25.         # create a new maildir mailbox to work with:
  26.         self._dir = test_support.TESTFN
  27.         os.mkdir(self._dir)
  28.         os.mkdir(os.path.join(self._dir, "cur"))
  29.         os.mkdir(os.path.join(self._dir, "tmp"))
  30.         os.mkdir(os.path.join(self._dir, "new"))
  31.         self._counter = 1
  32.         self._msgfiles = []
  33.  
  34.     def tearDown(self):
  35.         map(os.unlink, self._msgfiles)
  36.         os.rmdir(os.path.join(self._dir, "cur"))
  37.         os.rmdir(os.path.join(self._dir, "tmp"))
  38.         os.rmdir(os.path.join(self._dir, "new"))
  39.         os.rmdir(self._dir)
  40.  
  41.     def createMessage(self, dir):
  42.         t = int(time.time() % 1000000)
  43.         pid = self._counter
  44.         self._counter += 1
  45.         filename = os.extsep.join((str(t), str(pid), "myhostname", "mydomain"))
  46.         tmpname = os.path.join(self._dir, "tmp", filename)
  47.         newname = os.path.join(self._dir, dir, filename)
  48.         fp = open(tmpname, "w")
  49.         self._msgfiles.append(tmpname)
  50.         fp.write(DUMMY_MESSAGE)
  51.         fp.close()
  52.         if hasattr(os, "link"):
  53.             os.link(tmpname, newname)
  54.         else:
  55.             fp = open(newname, "w")
  56.             fp.write(DUMMY_MESSAGE)
  57.             fp.close()
  58.         self._msgfiles.append(newname)
  59.  
  60.     def test_empty_maildir(self):
  61.         """Test an empty maildir mailbox"""
  62.         # Test for regression on bug #117490:
  63.         # Make sure the boxes attribute actually gets set.
  64.         self.mbox = mailbox.Maildir(test_support.TESTFN)
  65.         self.assert_(hasattr(self.mbox, "boxes"))
  66.         self.assert_(len(self.mbox.boxes) == 0)
  67.         self.assert_(self.mbox.next() is None)
  68.         self.assert_(self.mbox.next() is None)
  69.  
  70.     def test_nonempty_maildir_cur(self):
  71.         self.createMessage("cur")
  72.         self.mbox = mailbox.Maildir(test_support.TESTFN)
  73.         self.assert_(len(self.mbox.boxes) == 1)
  74.         self.assert_(self.mbox.next() is not None)
  75.         self.assert_(self.mbox.next() is None)
  76.         self.assert_(self.mbox.next() is None)
  77.  
  78.     def test_nonempty_maildir_new(self):
  79.         self.createMessage("new")
  80.         self.mbox = mailbox.Maildir(test_support.TESTFN)
  81.         self.assert_(len(self.mbox.boxes) == 1)
  82.         self.assert_(self.mbox.next() is not None)
  83.         self.assert_(self.mbox.next() is None)
  84.         self.assert_(self.mbox.next() is None)
  85.  
  86.     def test_nonempty_maildir_both(self):
  87.         self.createMessage("cur")
  88.         self.createMessage("new")
  89.         self.mbox = mailbox.Maildir(test_support.TESTFN)
  90.         self.assert_(len(self.mbox.boxes) == 2)
  91.         self.assert_(self.mbox.next() is not None)
  92.         self.assert_(self.mbox.next() is not None)
  93.         self.assert_(self.mbox.next() is None)
  94.         self.assert_(self.mbox.next() is None)
  95.  
  96.     # XXX We still need more tests!
  97.  
  98.  
  99. def test_main():
  100.     test_support.run_unittest(MaildirTestCase)
  101.  
  102.  
  103. if __name__ == "__main__":
  104.     test_main()
  105.